#include "navigation_widget.h"

namespace widgets {

  Navigation::Navigation(QWidget *parent)
    : QWidget(parent)
  {
    ui.setupUi(this);
    ui.horizontalSlider->setMinimum(0);
    connect(ui.horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(on_frame_changed(int)));
    set_current_frame(0);
    QObject::connect(&timer_, SIGNAL(timeout()), this, SLOT(on_timer()));
  }

  Navigation::~Navigation()
  {

  }

  quint32 Navigation::get_frame_qty() const
  {
    return ui.horizontalSlider->maximum() + 1;
  }

  void Navigation::set_frame_qty(quint32 frame_qty)
  {
    ui.horizontalSlider->setMaximum(frame_qty - 1);
    ui.horizontalSlider->setTickInterval(frame_qty / 10);
    set_current_frame(0);
  }

  quint32 Navigation::get_current_frame() const
  {
    return ui.horizontalSlider->value();
  }

  void Navigation::set_current_frame(quint32 frame)
  {
    ui.horizontalSlider->setSliderPosition(frame);
    ui.label->setText(QString("%1").arg(frame * 1.017E-4));
//    ui.label->setText(QString("%1/%2").arg(frame + 1).arg(get_frame_qty()));
  }

  void Navigation::on_frame_changed(int frame)
  {
    emit frame_changed();
  }


  void Navigation::on_pushPlay_clicked()
  {
    if (timer_.isActive())
      timer_.stop();
    else
    {
      const quint32 frame = get_current_frame();
      if (frame >= get_frame_qty() - 1)
        set_current_frame(0);
      timer_.start(100);
    }
  }

  void Navigation::on_pushStop_clicked()
  {
    if (timer_.isActive())
      timer_.stop();
    else
      set_current_frame(0);
  }

  void Navigation::on_timer()
  {
    const quint32 frame = get_current_frame();

    if (frame >= get_frame_qty() - 1)
    {
      set_current_frame(0);
    }
    else
      set_current_frame(frame + 1);
  }

} // namespace widgets {